package com.blog.wu.myblog.base;

import com.blog.wu.myblog.util.Const;
import com.blog.wu.myblog.util.PageBean;
import com.blog.wu.myblog.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/**
 * 基础crud类
 *
 * @Date 2018/12/28 12:05 AM
 * @Author wuyanshen
 */
@CacheConfig(cacheNames = "baseCache")
@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class,readOnly = true)
public abstract class BaseCrudService<D extends CrudDao<T>, T extends DataEntity<T>> extends BaseService {

    /**
     * protected 访问范围：本类、本包、子类
     */
    @Autowired
    protected D dao;

    @Cacheable
    public T get(T entity) {
        return dao.get(entity);
    }
    @Cacheable
    public List<T> findList(T entity) {
        return dao.findList(entity);
    }

    @CacheEvict(allEntries = true)
    @Transactional(readOnly = false,rollbackFor = Exception.class)
    public Boolean update(T entity) {
        int count = dao.update(entity);
        if (count > 0) {
            return true;
        } else {
            return false;
        }
        
    }

    @CacheEvict(allEntries = true)
    @Transactional(readOnly = false,rollbackFor = Exception.class)
    public Boolean delete(T entity) {
        int count = dao.delete(entity);
        if (count > 0) {
            return true;
        }else {
            return false;
        }
    }

    @CacheEvict(allEntries = true)
    @Transactional(readOnly = false,rollbackFor = Exception.class)
    public Boolean add(T entity) {

        //手动调用设置id
        entity.preInsert();
        int count = dao.insert(entity);
        if (count > 0) {
            return true;
        }
        return false;
    }

    public Integer count(T entity) {
        return dao.count(entity).intValue();
    }


    public PageBean<T> findPage(T entity, Integer currentPageNo) {

        if (currentPageNo == null) {
            currentPageNo = 1;
        }

        //总记录数
        int allRow = count(entity);
        //总页数
        int totalPage = PageUtil.countTotalPage(allRow, Integer.parseInt(Const.PAGE_SIZE));
        //当前第几页
        int currentPage = PageUtil.currentPage(currentPageNo);
        //起始记录数
        int start = PageUtil.countStart(Integer.parseInt(Const.PAGE_SIZE), currentPage);

        if (null != currentPageNo || currentPageNo >= 0) {
            entity.setStart(start);
            entity.setLength(Integer.parseInt(Const.PAGE_SIZE));
        } else {
            entity.setStart(-1);
            entity.setLength(-1);
        }

        List<T> lists = dao.findPage(entity);

        PageBean<T> pageBean = new PageBean<>();
        pageBean.setAllRow(allRow);
        pageBean.setCurrentPage(currentPage);
        pageBean.setTotalPage(totalPage);
        pageBean.setList(lists);

        return pageBean;
    }




}
